package cn.edu.cqupt.controller;

import java.util.Date;
import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sun.org.apache.bcel.internal.generic.ReturnaddressType;

import cn.edu.cqupt.model.LoginUserInfo;
import cn.edu.cqupt.model.Loginuser;
import cn.edu.cqupt.model.ReturnInfo;
import cn.edu.cqupt.model.Userinfo;
import cn.edu.cqupt.service.HomeService;

@Controller
public class HomeControllerCb extends BaseController{
	
	private static Logger logger = LoggerFactory.getLogger(HomeControllerCb.class);
	
	@Resource
	private HomeService homeService;
	
	public HomeService getHomeService() {
		return homeService;
	}

	public void setHomeService(HomeService homeService) {
		this.homeService = homeService;
	}

	
	@RequestMapping("index")
	public String index(){
		System.out.println("进入到主页 ");
		return "login";
	}
	
	@RequestMapping("index/clerk.do")
	public String clerk(){
		System.out.println("进入到员工页面 ");
		return "employee_main";
	}
	
	@ResponseBody
	@RequestMapping(value="index/login",method=RequestMethod.POST)
	public ReturnInfo login(@RequestParam(value = "username", required = false, defaultValue = "null") String username,
			@RequestParam(value = "password", required = false, defaultValue = "null") String password,
			HttpServletRequest request){
		System.out.println("username>>>>>>"+username);
		System.out.println("password>>>>>>>>>>"+password);
	ReturnInfo info=new ReturnInfo();
	if ("null".equals(username)||"null".equals(password)){
		//提示用户用户名或者密码不能为空
		info.setInfo("用户名或密码不能为空");
		info.setStatus("0");
	}
	else {
		HashMap<String, Object> loginuser = null;
		try {
			loginuser=homeService.selectLoginuser(username,password);
			Loginuser user=null;
			Userinfo uinfo=null;
			if(loginuser!=null){
				user=(Loginuser) loginuser.get("loginuser");
				uinfo=(Userinfo) loginuser.get("userinfo");
				
			}
			
			info.setInfo("登陆成功！！");
			info.setStatus("1");
			info.setUrl("/BankSystem_16/index/clerk.do");
			HttpSession session=request.getSession();
			session.setAttribute("loginuser", user);
			session.setAttribute("userinfo", uinfo);
		} catch (Exception e) {
			info.setInfo(e.getMessage().toString());
			info.setStatus("0");
		}
	}
		return info;
	}
	@ResponseBody
	@RequestMapping(value="getuserinfo")
	public LoginUserInfo getuserinfo(){
		System.out.println("进入到请求。。。。");
		LoginUserInfo info=null;
		info=homeService.selectuserinfo("2014211514");
		return info;
	}
}
